<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="text" version="1.0" encoding="gb2312" indent="yes"/>

<xsl:template match="EntityMeta">
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.SessionState;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using OT.Common.UIPage;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxTreeList;
using DevExpress.XtraPrinting;
using OT.Module.Model;
using OT.Module.BLL;
using OT.BaseLibrary;
using OT.Common.Generic;
using OT.Module.Tree;
using OT.Common.Cache;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using DevExpress.Web.Data;
using DevExpress.Xpo;
using OT.Module.ModelConst;

public partial class UI_<xsl:value-of select="/EntityMeta/TableName" /> : DevPage
{
    /***
     *  Page
     */

    #region Page_Load

    protected void Page_Load(object sender, EventArgs e)
    {
        // 初始化树
        InitTree();

        if (!IsPostBack &amp;&amp; !IsCallback)
        {
            // 其它控件初始化
            InitControlValue();
            // 风格设置
            InitTreeStyle(treeList);
            InitGridStyle(InfoGrid, this.GetType().FullName);
        }
        // 调整分页风格
        GridPagerStyleSetting(InfoGrid, commonPageCtrl.PagerType);
    }

    #endregion

    #region InitControlValue

    protected void InitControlValue()
    {

        // 下面加入页面自定义控件初始化代码
    }

    #endregion

    /***
     *  Tree &amp; Panel &amp; Grid
     */

    #region InitTree

    private void InitTree()
    {
        if (treeList.Nodes.Count > 0)
            return;

        // TODO:调用TreeWay中合适函数建立树
    }

    #endregion

    #region ajaxPanel_Callback

    protected void ajaxPanel_Callback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        // 切换树节点时，要将列表之前选定的行取消
        InfoGrid.Selection.UnselectAll();
        // 数据源使用后期参数绑定，所以要调用DataBind
        InfoGrid.DataBind();
    }

    #endregion

    #region InfoGrid_PreRender

    protected void InfoGrid_PreRender(object sender, EventArgs e)
    {
        // 对列表的每一列进行自动的设置，包括
        // 1、根据字段长度自动调整列宽
        // 2、设置编辑页面的文本不换行
        AutoSettingGridColumnStyle(InfoGrid);
    }

    #endregion

    #region InfoGrid_CustomCallback

    protected void InfoGrid_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        // 统一处理新增，批量删除，改变分页风格，打开/关闭过滤行事件
        if (HandleDefaultEvent(e.Parameters, InfoGrid, new <xsl:value-of select="GenConfig/BllConfig/ClassName" />(XpoSession)))
            return;

        // 不能统一处理的事件，由页面自行处理
    }
    #endregion

    /***
     *  DataSource
     */

    #region @ dataSource_Selecting

    protected void dataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        // TODO:定制数据源获取数据时向Select函数提供的参数
        // e.InputParameters["xxx"] = the value;
    }

    #endregion

    #region ds_ObjectCreating

    protected void ds_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
    {
        e.ObjectInstance = new <xsl:value-of select="GenConfig/BllConfig/ClassName" />(XpoSession);
    }
    #endregion

	<!-- 根据外键字段建立数据源 -->
	<xsl:apply-templates select="/EntityMeta/Propertys/PropertyMeta" />
	
    /***
     *  Grid data event
     */

    #region @ InfoGrid_InitNewRow

    protected void InfoGrid_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e)
    {
        string treeKey = GetTreeFocusKey(treeList);
        <xsl:value-of select="GenConfig/MdlConfig/ClassName" /> obj = new <xsl:value-of select="GenConfig/BllConfig/ClassName" />(XpoSession)._SelectEntityByKey(treeKey);
        if (obj != null)
        {
            // 如果树节点的信息会影响新增记录的初始属性，则在这里初始化
			// e.NewValues.Add("PropertyName", value);
        }
    }

    #endregion

    #region InfoGrid_RowInserting

    protected void InfoGrid_RowInserting(object sender, ASPxDataInsertingEventArgs e)
    {
        <xsl:value-of select="GenConfig/MdlConfig/ClassName" /> entity = new <xsl:value-of select="GenConfig/MdlConfig/ClassName" />(XpoSession);

        new EntityMeta&lt;<xsl:value-of select="GenConfig/MdlConfig/ClassName" />&gt;(XpoSession).SetEntityProperty(entity, e.NewValues);

        new <xsl:value-of select="GenConfig/BllConfig/ClassName" />(XpoSession).Insert(entity);

        DataSourceRefreshCancel(InfoGrid, e);
    }
    #endregion

    #region InfoGrid_RowUpdating

    protected void InfoGrid_RowUpdating(object sender, ASPxDataUpdatingEventArgs e)
    {
        EntityMeta&lt;<xsl:value-of select="GenConfig/MdlConfig/ClassName" />&gt; meta = new EntityMeta&lt;<xsl:value-of select="GenConfig/MdlConfig/ClassName" />&gt;(XpoSession);

        <xsl:value-of select="GenConfig/MdlConfig/ClassName" /> entity = XpoSession.GetObjectByKey&lt;<xsl:value-of select="GenConfig/MdlConfig/ClassName" />&gt;(e.Keys[meta.KeyProperty].ToString());
        <xsl:value-of select="GenConfig/MdlConfig/ClassName" /> old = XpoSession.GetObjectByKey&lt;<xsl:value-of select="GenConfig/MdlConfig/ClassName" />&gt;(e.Keys[meta.KeyProperty].ToString());

        meta.SetEntityProperty(entity, e.NewValues);

        new <xsl:value-of select="GenConfig/BllConfig/ClassName" />(XpoSession).Update(entity, old);

        DataSourceRefreshCancel( InfoGrid , e);
    }
    #endregion

    #region InfoGrid_RowDeleting
    protected void InfoGrid_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
        string id = e.Keys[0].ToString();
        
        string error = "";

        // 未通过删除校验则取消随后发送到数据逻辑层的操作
        if (! new <xsl:value-of select="GenConfig/BllConfig/ClassName" />(XpoSession)._CheckDelete(id, ref error))
        {
            throw new ApplicationException(error);
        }

        new <xsl:value-of select="GenConfig/BllConfig/ClassName" />(XpoSession)._DeleteByKey(id);

        DataSourceRefreshCancel(InfoGrid, e);
    }
    #endregion
    /***
     *  Validate
     */

    #region InfoGrid_RowValidating

    protected void InfoGrid_RowValidating(object sender, ASPxDataValidationEventArgs e)
    {
        <xsl:value-of select="GenConfig/MdlConfig/ClassName" /> entity = new <xsl:value-of select="GenConfig/MdlConfig/ClassName" />(XpoSession);

        new EntityMeta&lt;<xsl:value-of select="GenConfig/MdlConfig/ClassName" />&gt;(XpoSession).SetEntityProperty(entity, e.NewValues);

        ValidationMDL&lt;<xsl:value-of select="GenConfig/MdlConfig/ClassName" />&gt;(entity, InfoGrid, e);
    }

    #endregion

    /***
     *  Export
     */

    #region btnExport_Click

    protected void btnExport_Click(object sender, EventArgs e)
    {
        // 将表格数据导出
        Export(commonPageCtrl.GridExpoter, commonPageCtrl.FileType);
    }

    #endregion

}
</xsl:template>

<xsl:template match="/EntityMeta/Propertys/PropertyMeta">
<xsl:if test="IsForigenKey='true'">
	#region ds<xsl:value-of select="MasterTable" />_ObjectCreating

    protected void ds<xsl:value-of select="MasterTable" />_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
    {
        e.ObjectInstance = new <xsl:value-of select="MasterTable" />_BLL(XpoSession);
    }
    #endregion
</xsl:if>
<xsl:if test="IsEnum='true'">
    #region ds_Enum<xsl:value-of select="EnumType" />Creating

    protected void ds_Enum<xsl:value-of select="EnumType" />Creating(object sender, ObjectDataSourceEventArgs e)
    {
        e.ObjectInstance = new EnumDataSource(typeof(<xsl:value-of select="EnumType" />));
    }
    #endregion
</xsl:if>
</xsl:template>

</xsl:stylesheet>